import apis from '../../http/apis.js'

export default {
    namespaced: true,
    state: {
        allOrders: [],
        pages: {
            pageSize: 5,  //每页显示条数
            currentPage: 1,  //当前页
            total: 5, //总条数
            pageTotal: 1 //总页数
        },

        orderData: {
            orderState: 1,
            cancellation: 0,
            insuresId: '',
            _id: '',
            orderTime: '2021-1-6',
            usersId: ''
        }
    },
    mutations: {
        changeAllOrders(state, data) {
            state.allOrders = data
            // console.log(state.allOrders,'11');
           
        },
        changePages(state, data) {
            state.total = data.total;
            state.pageTotal = data.totalPages
        },
        changeOrderList(state, data) {
            state.orderData.orderState = data.orderState;
            state.orderData.cancellation = data.cancellation;
            state.orderData.insuresId = data.insuresId;
            state.orderData.orderTime = data.orderTime;
            state.orderData.usersId = data.usersId;
            state.orderData._id = data._id;
            // console.log(state.orderData,'222');
        },
    },
    actions: {
        async getAllOrders(context) {
            const { currentPage, pageSize } = context.state.pages
            const { data } = await apis.orders.getOrders({ currentPage, pageSize })
            console.log(data);
            if (data.code) {
                context.commit('changeAllOrders', data.data.data)
                context.commit('changePages', data.data)

            }
        },

        async getSurrenderOrders(context) {
            const { data } = await apis.orders.getSurrenderOrders()
            console.log(data);
            if (data.code) {
                context.commit('changeAllOrders', data.data)
            }
        },

        //同意退保
        async handelSurrenderOrders(context) {
            const { data } = await apis.orders.handelSurrenderOrders()
            // console.log(data);
            if (data.code) {
                context.dispatch('getAllOrders', data.data)
                context.dispatch('getSurrenderOrders', data.data)
            }
        },
        //拒绝退保
        async refuseSurrenderOrders(context) {
            const { data } = await apis.orders.refuseSurrenderOrders()
            // console.log(data);
            if (data.code) {
                context.dispatch('getAllOrders', data.data)
                context.dispatch('getSurrenderOrders', data.data)
            }
        },
        async delOrdersById(context, _id) {
            const { data } = await apis.orders.delOrdersById({ _id })
            if (data.code) {
                context.dispatch('getAllOrders')
            }
        },

        async getOneOrderById(context, _id) {
            const { data } = await apis.orders.getOneOrderById({ _id })
            if (data.code) {
                // console.log('信息获取成功', data.data);
                context.commit('changeOrderList', data.data[0])
            }
        },

        async getSaleManOrder(context, _id) {
            const { data } = await apis.orders.getSaleManOrder({ _id })
            // console.log(data);
            if (data.code) {
                // console.log('销售员订单信息获取成功', data.data);
                context.commit('changeAllOrders', data.data)
            }
        },

        async updateOrderBySaleMan(context,{ _id,orderState,cancellation }) {
            // console.log({ _id,orderState,cancellation },'xiugai');
            const { data } = await apis.orders.updateOrderBySaleMan({ _id,orderState,cancellation })
            // console.log(data);
            if (data.code) {
                // console.log('销售员修改订单成功', data.data);
                context.commit('changeAllOrders', data.data)
            }
        },
    },
    modules: {
    }
}